﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;
using MESCodeFile;

namespace mes_tublay
{
    public partial class frmMyProfile : Form
    {
        CodeProcessor proc = new CodeProcessor();
        bool showingMore = false;

        public frmMyProfile()
        {
            InitializeComponent();
            LoadProfile();
            GUIEvents();
            ProcEvents();
        }
        void ProcEvents()
        {
            proc.ProcessCompleted += new EventHandler(delegate
            {
                proc.SafeCall(this, delegate
                {
                    if (proc.State == ProcessorFlag.DataRdyErr ||
                        proc.State == ProcessorFlag.Error ||
                        proc.State == ProcessorFlag.ReadyErr)
                    {
                        MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        //Function specific action
                        switch (proc.GetLastProcessName())
                        {
                            case "SAVE":
                                pnlPassword.Enabled = true;
                                txtPWDCur.Text = "";
                                txtPWDCon.Text = "";
                                txtPWDNew.Text = "";
                                break;
                        }
                        return;

                    }
                    switch (proc.GetLastProcessName())
                    {
                        case "SAVE":
                            txtPWDCur.Text = "";
                            txtPWDCon.Text = "";
                            txtPWDNew.Text = "";
                            this.Top = this.Top + 106;
                            btnMore.Top = 212;
                            this.Height = 212 + btnMore.Height + 30;
                            pnlPassword.Enabled = false;
                            showingMore = false;
                            break;
                    }
                });
            });
        }
        void GUIEvents()
        {
            btnMore.Click += new EventHandler(delegate
            {
                if (!showingMore)
                {
                    this.Top = this.Top - 106;
                    btnMore.Top = pnlPassword.Top + pnlPassword.Height + 2;
                    this.Height = btnMore.Top + btnMore.Height + 30;
                    pnlPassword.Enabled = true;
                    showingMore = true;
                }
                else
                {
                    this.Top = this.Top + 106;
                    btnMore.Top = 212;
                    this.Height = 212 + btnMore.Height + 30;
                    pnlPassword.Enabled = false;
                    showingMore = false;
                }
            });
            btnSave.Click += new EventHandler(delegate
            {
                if (txtPWDCur.Text != CurrentUser.PWD || txtPWDNew.Text != txtPWDCon.Text)
                {
                    lblMsg.Text = "Password mismatch!";
                    return;
                }
                pnlPassword.Enabled = false;

                CodeScript s = new CodeScript("SAVE");

                s.Add(CodeIns.SYS_RESET);
                s.Add(CodeIns.DBU_NEW_CORE);
                s.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"),
                        Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
                s.Add(CodeIns.DBU_BEGIN_TRANSACTION);
                s.Add(CodeIns.DBU_UPDATE, "tblusers", new string[] { "fPWD" }, new object[] { txtPWDNew.Text },
                    "(fID='" + CurrentUser.ID + "') AND (fStamp='" + Helper.ToMySQLDateTime(CurrentUser.STAMP) + "')",
                    new bool[] { true });
                s.Add(CodeIns.DBU_END);
                proc.Run(s);
            });
        }
        void LoadProfile()
        {
            string uid = CurrentUser.UID;
            if (CurrentUser.NAME.Trim() != "") uid = CurrentUser.NAME;
            txtUID.Text = uid;
            lstPriv.Items.Clear();
            lstPriv.Items.AddRange(CurrentUser.PRIV);
            imgUser.Image = CurrentUser.IMG;
        }
    }
}
